﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Portal.Logic.Creators;
using Portal.Logic.Providers;
using Portal.Logic.Services;
using Portal.Logic.Tests.Units;
using Rhino.Mocks;
using System.Security.Principal;
using Portal.Model;

namespace Portal.Logic.Providers.Tests.Units
{
	[TestClass()]
	public class UserProviderTests : BaseTests
	{
		private IUserService _service;
		private IUserCreator _cretor;

		protected override void InitializeTest()
		{
			_service = Mocks.DynamicMock<IUserService>();
			_cretor = Mocks.DynamicMock<IUserCreator>();
		}

		#region GetUser
		[TestMethod()]
		public void GetUser_IdentityIsNull_ReturnsNull()
		{
			Mocks.ReplayAll();

			var provider = GetProvider();
			var result = provider.GetUser(null);

			Assert.IsNull(result);
		}

		[TestMethod()]
		public void GetUser_UserIsNotFound_NewUserIsReturned()
		{
			var logon = "logon";
			var user = Mocks.DynamicMock<IUser>();

			Expect.Call(_service.GetBy(logon)).Return(user);

			Mocks.ReplayAll();

			var provider = GetProvider();
			var result = provider.GetUser(logon);

			Assert.AreEqual(user, result);
			Mocks.VerifyAll();
		}

		[TestMethod()]
		public void GetUser_UserIsFound_ExistingUserIsReturned()
		{
			var logon = "logon";
			var user = Mocks.DynamicMock<IUser>();

			Expect.Call(_service.GetBy(logon)).Return(null);
			Expect.Call(_cretor.From(logon)).Return(user);
			Expect.Call(_service.AddOrUpdate(user)).Return(user);

			Mocks.ReplayAll();

			var provider = GetProvider();
			var result = provider.GetUser(logon);

			Assert.AreEqual(user, result);
			Mocks.VerifyAll();
		} 
		#endregion

		private UserProvider GetProvider() {
			return new UserProvider(_service, _cretor);
		}
	}
}